package com.feihong.yw189.utils;

import com.feihong.yw189.app.App;
import com.pddstudio.preferences.encrypted.EncryptedPreferences;

/**
 * Created by Sabirjan on 2017/6/15.
 * TXGW.
 * 飞宏电子
 * 配置文件保存类(加密保存)
 * String ---> putString getString
 * boolean---> putBoolean getBoolean
 * int    ---> putInt     getInt
 * float  ---> putFloat   getFloat
 * long   ---> putLong    getLong
 */

public class PreferenceTools {
    /**
     * @param key
     * @param value
     * @return 成功返回true ，否则返回false
     */
    public static boolean putString(String key, String value) {
        if (App.getApp().getEncryptedPreferences() == null) {
            return false;
        }
        EncryptedPreferences.EncryptedEditor editor = App.getApp().getEncryptedPreferences().edit();
        editor.putString(key, value);
        editor.apply();
        return editor.commit();
    }

    public static String getString(String key, String defvalue) {
        if (App.getApp().getEncryptedPreferences() == null) {
            return defvalue;
        }
        return App.getApp().getEncryptedPreferences().getString(key, defvalue);
    }


    /**
     * @param key
     * @param value
     * @return 成功返回true ，否则返回false
     */
    public static boolean putBoolean(String key, boolean value) {
        if (App.getApp().getEncryptedPreferences() == null) {
            return false;
        }
        EncryptedPreferences.EncryptedEditor editor = App.getApp().getEncryptedPreferences().edit();
        editor.putBoolean(key, value);
        editor.apply();
        return editor.commit();
    }

    public static boolean getBoolean(String key, boolean defvalue) {
        if (App.getApp().getEncryptedPreferences() == null) {
            return defvalue;
        }
        return App.getApp().getEncryptedPreferences().getBoolean(key, defvalue);
    }
    /**
     * @param key
     * @param value
     * @return 成功返回true ，否则返回false
     */
    public static boolean putFloat(String key, float value) {
        if (App.getApp().getEncryptedPreferences() == null) {
            return false;
        }
        EncryptedPreferences.EncryptedEditor editor = App.getApp().getEncryptedPreferences().edit();
        editor.putFloat(key, value);
        editor.apply();
        return editor.commit();
    }

    public static float getFloat(String key, float defvalue) {
        if (App.getApp().getEncryptedPreferences() == null) {
            return defvalue;
        }
        return App.getApp().getEncryptedPreferences().getFloat(key, defvalue);
    }

    /**
     * @param key
     * @param value
     * @return 成功返回true ，否则返回false
     */
    public static boolean putInt(String key, int value) {
        if (App.getApp().getEncryptedPreferences() == null) {
            return false;
        }
        EncryptedPreferences.EncryptedEditor editor = App.getApp().getEncryptedPreferences().edit();
        editor.putInt(key, value);
        editor.apply();
        return editor.commit();
    }

    public static int getInt(String key, int defvalue) {
        if (App.getApp().getEncryptedPreferences() == null) {
            return defvalue;
        }

        return App.getApp().getEncryptedPreferences().getInt(key, defvalue);
    }

    /**
     * @param key
     * @param value
     * @return 成功返回true ，否则返回false
     */
    public static boolean putLong(String key, long value) {
        if (App.getApp().getEncryptedPreferences() == null) {
            return false;
        }
        EncryptedPreferences.EncryptedEditor editor = App.getApp().getEncryptedPreferences().edit();
        editor.putLong(key, value);
        editor.apply();
        return editor.commit();
    }

    public static long getLong(String key, long defvalue) {
        if (App.getApp().getEncryptedPreferences() == null) {
            return defvalue;
        }
        return App.getApp().getEncryptedPreferences().getLong(key, defvalue);
    }

    public static boolean clear(){
        EncryptedPreferences.EncryptedEditor editor = App.getApp().getEncryptedPreferences().edit();
        return editor.clear().commit();
    }

}
